package com.cloud.code.牛客网;

import java.util.*;

public class Test67 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){

            Map<String,Integer> map = new LinkedHashMap<>();

            int num = Integer.parseInt(scanner.nextLine());
            int flag = Integer.parseInt(scanner.nextLine());

            for (int i = 0; i < num; i++) {
                String line = scanner.nextLine();
                String[] s = line.split(" ");
                map.put(s[0],Integer.parseInt(s[1]));
            }

            List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());

            list.sort(new Comparator<Map.Entry<String,Integer>>() {
                @Override
                public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {

                    if(flag == 0){
                        return o2.getValue()-o1.getValue();
                    }else {
                        return o1.getValue()-o2.getValue();
                    }
                }
            });

            list.forEach(stringIntegerEntry -> {
                System.out.println(stringIntegerEntry.getKey()+" "+stringIntegerEntry.getValue());
            });
        }
    }
}
